<?php
/**
 * Created by PhpStorm.
 * User: 子不语
 * Date: 2016/6/27
 * Time: 12:24
 */
if (!defined('KKINC'))	 exit('Request Error!') ;
class sms
{
   

    function count($mobile){
        $sql = "select count(*) from validcodelog where code_type = 0 and state = 0 and account = '{$mobile}' and to_days(add_time) = to_days(now())";
        return $total=db::get_total($sql);
    }
    function get_code($mobile){
        $sql = "select code from validcodelog where code_type = 0 and state = 0 and account = '{$mobile}' and to_days(add_time) = to_days(now()) order by id desc limit 1";
        $row=db::get_one($sql,'assoc');
        return $row['code'];
    }
    function is_has_usr($username){
        $sql="select id from users where username='$username'";
        $row=db::get_one($sql,'assoc');
        return $row['id'];
    }
    function send($mobile){
        $validcode = rand(1000,9999);
        $SMS =  "您的验证码是：{$validcode}。请不要把验证码泄露给其他人。如非本人操作，可不用理会！";
        $sM = ($mobile);
        $sC = ($SMS);
        $sql = "insert into validcodelog(userid,code_type,account,content,add_time,state,code)
				values(0,0,'{$sM}','{$sC}',now(),0,{$validcode})";
        $result =  db::_query($sql);
        $insertID =db::last_id();
        $smsReturn = $this->PostSMS($mobile,$SMS);
        if($smsReturn == "ok")
        {
            $_SESSION['mobilesmscode'] = $validcode;
            return true;
        }
        else
        {
            $sR = ($smsReturn);
            $sql = "update validcodelog set state = 1,err_msg='{$sR}' where id = {$insertID}";
            $result =  db::_query($sql);
            return false;
        }
        return false;
    }
    function PostSMS($mobile,$content)
    {
        $target = "http://sms.106jiekou.com/utf8/sms.aspx";
        //替换成自己的测试账号,参数顺序和wenservice对应
        $post_data = "account=robinson&password=qq52013145678&mobile={$mobile}&content=".rawurlencode($content);

        $smsResult = $this->Post($post_data, $target);
        $ret = "ok";
        switch($smsResult)
        {
            case "100":
                $ret = "ok";
                break;
            case "101":
                $ret = "验证失败";
                break;
            case "102":
                $ret = "手机号码格式不正确";
                break;
            case "103":
                $ret = "会员级别不够";
                break;
            case "104":
                $ret = "内容未审核";
                break;
            case "105":
                $ret = "内容过多";
                break;
            case "106":
                $ret = "账户余额不足";
                break;
            case "107":
                $ret = "Ip受限";
                break;
            case "108":
                $ret = "手机号码发送太频繁，请换号或隔天再发";
                break;
            case "109":
                $ret = "帐号被锁定";
                break;
            case "110":
                $ret = "发送通道不正确";
                break;
            case "111":
                $ret = "当前时间段禁止短信发送";
                break;
            case "120":
                $ret = "系统升级";
                break;
            default:
                $ret = "未知错误:" . $smsResult;
                break;
        }
        return $ret;
    }

    function Post($curlPost,$url){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_NOBODY, true);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
        $return_str = curl_exec($curl);
        curl_close($curl);
        return $return_str;
    }

}